<?php
require_once '../../database/config.php';

class classify
{
    /**
     * 获取试题归类列表信息（分页提取）
     * param: pageIndex（当前页码）
     * param: pageSize（每页记录数）
     * param：classifyname（查询条件归类名称）
     */
    function getClassifyListByPage($pageIndex, $pageSize, $classifyname)
    {
        $sql = "select * from classify where 1=1 ";
        if ($classifyname != "") {
            $sql = $sql . " and classifyname like '%$classifyname%'";
        }
        $count = $this->getClassifyListCount($sql);
        $startno = ($pageIndex - 1) * $pageSize;
        $sql = $sql . " order by inputdate desc limit $startno," . $pageSize;
        $result = $GLOBALS['conn']->query($sql);
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $classifyList = array();
        while ($row = $result->fetch()) {
            $classifyList[] = $row;
        }
        $result = array(
            "code" => 0,
            "count" => $count,
//            "msg" => "success",
            "list" => $classifyList
        );
        echo json_encode($result);
    }

    /**
     * 获取记录总数（用于实现分页查询）
     * param: sql（查询语句）
     */
    function getClassifyListCount($sql)
    {
        $result = $GLOBALS['conn']->query($sql);
        return $result->rowCount();
    }

    /**
     * 添加试题归类
     * param：classifyid（试题归类编号）
     * param：classifyname（试题归类名称）
     * param：memo（说明）
     */

    function addClassify($classifyid, $classifyname, $memo)
    {
        if ($this->checkClassifyid($classifyid)) {
            $stmt = $GLOBALS['conn']->prepare("insert into classify(classifyid,classifyname,memo,inputdate) values(:classifyid,:classifyname,:memo,now())");
            $stmt->bindParam("classifyid", $classifyid);
            $stmt->bindParam("classifyname", $classifyname);
            $stmt->bindParam("memo", $memo);
            $rs = $stmt->execute();
            if ($rs) {
                //获取服务器时间
                $result = $GLOBALS['conn']->query("select DATE_FORMAT(now(),'%Y-%m-%d') as inputdate");
                $result->setFetchMode(PDO::FETCH_ASSOC);
                $inputdate = '';
                if ($row = $result->fetch()) {
                    $inputdate =  $row['inputdate'];
                }
                $result = array("msg" => "操作成功", "errorCode" => 0, "inputdate"=>$inputdate);
            } else {
                $result = array("msg" => "操作失败", "errorCode" => 1);
            }
            echo json_encode($result);
        }else{
            $result = array("msg" => "题型编号重复", "errorCode" => 2);
            echo json_encode($result);
        }
    }

    /**
     * 检查归类编号是否重复
     * param：classifyid（归类编号）
     */
    function checkClassifyid($classifyid)
    {
        $stmt = $GLOBALS['conn']->prepare("select * from classify where UPPER(classifyid)=:classifyid");
        $stmt->bindParam('classifyid', strtoupper($classifyid));
        $stmt->execute();
        return $stmt->rowCount() == 0 ? true:false;
    }

    /**
     * 根据归类编号查询
     * param：classifyid（归类编号）
     */
    function getClassifyById($classifyid)
    {
        $stmt = $GLOBALS['conn']->prepare("select * from classify where classifyid=:classifyid");
        $stmt->bindParam('classifyid', strtoupper($classifyid));
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $result = array();
        if ($rows = $stmt->fetch()){
            $result[] = $rows;
        }
        echo json_encode($result);
    }
    /**
     * 修改试题归类
     * param：classifyid（归类编号）
     * param：classifyname（归类名称）
     * param：memo（说明）
     */
    function updateClassify($classifyid, $classifyname, $memo){
        $stmt = $GLOBALS['conn']->prepare("update classify set classifyname=:classifyname,memo=:memo where classifyid=:classifyid");
        $stmt->bindParam("classifyid", $classifyid);
        $stmt->bindParam("classifyname", $classifyname);
        $stmt->bindParam("memo", $memo);
        $rs = $stmt->execute();
        if ($rs) {
            $result = array("msg" => "操作成功", "errorCode" => 0);
        } else {
            $result = array("msg" => "操作失败", "errorCode" => 1);
        }
        echo json_encode($result);
    }

    /**
     * 删除归类
     * param：classifyid（归类编号）
     */
    function deleteClassify($classifyid){
        $stmt = $GLOBALS['conn']->prepare("delete from classify where classifyid=:classifyid  and classifyid not in (select classifyid from question)");
        $stmt->bindParam("classifyid", $classifyid);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            $result = array("msg" => "操作成功", "errorCode" => 0);
        } else {
            $result = array("msg" => "操作失败", "errorCode" => 1);
        }
        echo json_encode($result);
    }

}

$classify = new classify();
$method = $_POST['method'];
if ($method == "getClassifyListByPage") {
    $pageIndex = $_POST['pageIndex'];
    $pageSize = $_POST['pageSize'];
    $classifyname = $_POST['classifyname'];
    $classify->getClassifyListByPage($pageIndex, $pageSize, $classifyname);
}
if ($method == "addClassify") {
    $classifyid = $_POST['classifyid'];
    $classifyname = $_POST['classifyname'];
    $memo = $_POST['memo'];
    $classify->addClassify($classifyid, $classifyname, $memo);
}
if ($method == "getClassifyById") {
    $classifyid = $_POST['classifyid'];
    $classify->getClassifyById($classifyid);
}
if ($method == "updateClassify") {
    $classifyid = $_POST['classifyid'];
    $classifyname = $_POST['classifyname'];
    $memo = $_POST['memo'];
    $classify->updateClassify($classifyid, $classifyname, $memo);
}
if ($method == "deleteClassify") {
    $classifyid = $_POST['classifyid'];
    $classify->deleteClassify($classifyid);
}
?>